#include <iostream>
#include "CFileRead.h"
#include "CAsciiFileRead.h"
#include "CBinaryFileRead.hpp"
#include "CAbstractFileReadFactory.h"
#include "CAsciiFileReadFactory.h"
#include "CBinaryFileReadFactory.hpp"

// test exa: for CAsciiFileRead
void test()
{
    CAsciiFileRead testFile("./data/test.txt", 40);
    CFileRead *pFile = &testFile;
    testFile.skipLines(1); // 指向派生类的基类指针只能调用基类的成员函数和成员变量
    pFile->loadAll();
    pFile->Load();
}

// test exa: for CBinaryFileRead
class MyData
{
public:
    double d1;
    double d2;
    double d3;
    double d4;
};

void func(CFileRead *file_read)
{
    file_read->loadAll();
    file_read->Load();
}

void test01()
{
    CFileRead *p_ascii_file_reader = new CAsciiFileRead("./data/test.txt", 40);
    CFileRead *p_bin_file_reader = new CBinaryFileRead<MyData>("./data/students.dat");
    func(p_ascii_file_reader);
    func(p_bin_file_reader);
    delete p_ascii_file_reader;
    delete p_bin_file_reader;
}

void func02(CFileRead &file_read)
{
    file_read.loadAll();
    file_read.Load();
}

void test02()
{
    CAsciiFileRead ascii_file_reader("./data/test.txt", 40);
    CBinaryFileRead<MyData> bin_file_reader("./data/students.dat");
    func02(ascii_file_reader);
    func02(bin_file_reader);
}

void func03(CFileRead *file_read)
{
    file_read->loadAll();
    file_read->Load();
}

void testFactoryMethod()
{
    
    CAbstractFileReadFactory *factory_ptr = new CAsciiFileReadFactory;
    CFileRead* file_read_ptr =  factory_ptr->creatFileRead();
    func03(file_read_ptr);

    factory_ptr = new CBinaryFileReadFactory<MyData>;
    file_read_ptr =  factory_ptr->creatFileRead();
    func03(file_read_ptr);

    delete file_read_ptr;
    delete factory_ptr;

}

// ************************main************************* //
int main(int argc, char **argv)
{
    // test();
    // test01();
    // test02();
    testFactoryMethod();
    return 0;
}